package com.tsing.bms.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tsing.bms.entity.Order;
import com.tsing.bms.vo.req.OrderPageReqVo;
import com.tsing.bms.vo.resp.OrderPageRespVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author TheTsing
 * @since 2021-03-29
 */
public interface OrderMapper extends BaseMapper<Order> {

    @Select("<script>" +
            "select o.*,h.name houseName,u.nick_name householdName\n" +
            "from t_order o\n" +
            "left join t_house h on o.house_id = h.id\n" +
            "left join t_user u on h.household_id = u.id\n" +
            "<where>\n" +
            "<if test=\"reqVo.type!=null and reqVo.type!=''\">and o.type = #{reqVo.type}</if>\n" +
            "<if test=\"reqVo.householdName!=null and reqVo.householdName!=''\">and u.nick_name like concat('%',#{reqVo.householdName},'%')</if>\n" +
            "</where>\n" +
            "order by o.id desc" +
            "</script>")
    List<OrderPageRespVo> orderPage(OrderPageReqVo reqVo, Page<Order> page);

    @Select("select count(o.id)\n" +
            "from t_order o \n" +
            "inner join t_house h on o.house_id=h.id\n" +
            "where h.household_id=#{id} and o.pay=0 and type=#{i}")
    int orderCount(@Param("id") String id, @Param("i") int i);

}
